#ifndef QRSAGSQUERYDIALOG_H
#define QRSAGSQUERYDIALOG_H

#include <QDialog>
#include <QProgressDialog>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>

#include "qgsvectorlayer.h"
#include "qgsattributetableview.h"
#include "qgis_sip.h"
#include "qgis_gui.h"
#include "qgsexpressioncontext.h"
#include "qgsexpression.h"
#include "qgsexpressiontreeview.h"
#include "qgsattributeform.h"
#include "qgsattributetablefiltermodel.h"
#include "qgsrubberband.h"

class QgsFields;
class QgsExpressionHighlighter;
class QgsRelation;

namespace Ui {
class QRSAGSQueryDialog;
}

class QRSAGSQueryDialog : public QDialog
{
    Q_OBJECT

public:
    explicit QRSAGSQueryDialog(QgsVectorLayer *currentVectorLayer, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::Window);
    ~QRSAGSQueryDialog();

    /**
     * The set expected format string. This is pure text format and no expression validation
     * is done against it.
     * \param expected The expected value format for the expression.
     * \note Only a UI hint and not used for expression validation.
     */
    void setExpectedOutputFormat( const QString &expected );

    /**
     * Set the filter mode
     *
     * \param filterMode
     */
    void setFilterMode( QgsAttributeTableFilterModel::FilterMode filterMode );
signals:
    /**
     * Emitted when the user changes the expression in the widget.
     * Users of this widget should connect to this signal to decide if to let the user
     * continue.
     * \param isValid Is TRUE if the expression the user has typed is valid.
     */
    void expressionParsed( bool isValid );

    /**
     * Will be set to TRUE if the current expression text reported an eval error
     * with the context.
     *
     * \since QGIS 3.0
     */
    void evalErrorChanged();

    /**
     * Will be set to TRUE if the current expression text reported a parser error
     * with the context.
     *
     * \since QGIS 3.0
     */
    void parserErrorChanged();
    /**
     * Emitted whenever the filter changes
     */
    void filterChanged();


private:
    class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
    {
    public:
        ExpressionTreeMenuProvider( QRSAGSQueryDialog *queryDialog )
            : QgsExpressionTreeView::MenuProvider()
            , mQueryDialog( queryDialog ) {}

        //        QMenu *createContextMenu( QgsExpressionItem *item ) override;

    private:
        QRSAGSQueryDialog *mQueryDialog;
    };


    Ui::QRSAGSQueryDialog *ui;

    int FUNCTION_MARKER_ID = 25;

    QgsVectorLayer *mVectorLayer = nullptr;
    bool mExpressionValid = false;
    ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
//    std::unique_ptr<QStandardItemModel> mValuesModel;
//    std::unique_ptr<QSortFilterProxyModel> mProxyValues;
    QgsExpressionContext mExpressionContext;
    QgsAttributeTableFilterModel *mFilterModel = nullptr;
    QgsAttributeTableModel *mTableModel = nullptr;
    QgsMapCanvas *mMapcanvas = nullptr;
    QgsRubberBand *mRubberBand = nullptr, *mSingleRubberBand = nullptr;

    //初始化图表内容
    void initTableView();
    void initExpressionBuilderByParameter();
    void initExpressionBuilderByDetailedParameter();
    void initExpressionBuilderByPosition();

    void clearErrors();//
    void createErrorMarkers( QList<QgsExpression::ParserError> errors );//
    void createMarkers( const QgsExpressionNode *node );//
    //    void clearFunctionMarkers();
    //    void clearErrors();
    //    void runPythonCode( const QString &code );
    //    void fillFieldValues( const QString &fieldName, int countLimit, bool forceUsedValues = false );

    void setExpressionContext( const QgsExpressionContext &context );//
    void setLayer( QgsVectorLayer *layer );//
    void setExpressionText( const QString &expression );//
    void expressionContextUpdated();//

    bool isDigitStr(QString value);
private slots:
    //    void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
    void onExpressionParsed( bool state );//
    void operatorButtonClicked();//
    //    void btnRun_pressed();
    //    void btnNewFile_pressed();
    //    void btnRemoveFile_pressed();

    //    void importUserExpressions_pressed();
    //    void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
    void insertExpressionText( const QString &text );//
    void txtExpressionString_textChanged();//
    void queryExpressionChanged(QString);//
    //    void txtSearchEditValues_textChanged();
    //    void mValuesListView_doubleClicked( const QModelIndex &index );
    //    void txtPython_textChanged();

    //按照属性查询
    void queryByDetailedParameterExpression(bool);
    void queryByParameterExpression(bool);
    void clearFilterText(bool);
    void clearDetailedFilterText(bool);
    void extentChanged();
    void filterError( const QString &errorMessage );
    void updateSelectedFeatures();
    void zoomToCurrentFeature();
    void hightLightFilteredFeatures(const QgsFeatureIds &featureIds);
    void hightLightSelectedFeature(const QModelIndex &index);

    //按照位置查询
    void queryByRectanglePosition();
    void queryByPointPosition();
    void pointPositionValueChanged(QString value);
    void rectanglePositionValueChanged(QString value);
    void clearPointPosition(bool);
    void clearRectanglePosition(bool);

    void closeDialog(bool);
};

#endif // QRSAGSQueryDialog_H
